﻿#define _CRT_SECURE_NO_WARNINGS
#include "san-zi-qi.h"

//
//实现初始化棋盘
void Initborad(char borad[Hang][Lie],int hang,int lie)
{
	int h = 0,l=0;
	for (h = 0; h < hang; h++)
	{
		for (l = 0; l < lie; l++)
		{
			borad[h][l] = ' ';
		}
	}

}


//实现打印棋盘
void Displayborad(char borad[Hang][Lie], int hang, int lie)
{
	int h = 0, l = 0;
	for (h = 0; h < hang; h++)
	{
		for (l = 0; l < lie; l++)
		{
			printf(" %c ", borad[h][l]);
			if (l < lie - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (h < hang - 1)
		{
			for (l = 0; l < lie; l++)
			{
				printf("---");
				if (l < lie - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}

}


//玩家下棋
void Playertime(char borad[Hang][Lie], int hang, int lie)
{
	int h = 0, l = 0;
	printf("玩家请下棋:");
again1:
	scanf("%d%d", &h, &l);
	if (borad[h - 1][l - 1] == ' ')
	{
		borad[h - 1][l - 1] = '*';
		Displayborad(borad, hang, lie);
	}
	else
	{
		printf("输入的坐标有误，请重新输入:");
		goto again1;
	}
}


//电脑下棋
void Computertime(char borad[Hang][Lie], int hang, int lie)
{
	printf("电脑下棋:\n");
again2:
	int h = rand() % hang + 1;
	int l = rand() % lie + 1;
	if (borad[h - 1][l - 1] == ' ')
	{
		borad[h - 1][l - 1] = '#';
		Displayborad(borad, hang, lie);
	}
	else
	{
		goto again2;
	}


}


//判断玩家或电脑是否获胜
void Over(char borad[Hang][Lie], int hang, int lie,int*x)
{
	int h = 0;
	int l = 0;
	int y = 0;
	for (h = 0; h < Hang; h++)
	{
		char a = borad[h][0];
		char b = borad[h + 1][0];
		int i = 1;
		while (i)
		{
			Compare(a, b, &i, h,&y);
			if (y == 1)
			{
				*x = 1;
			}
			if (h + 1 == Hang)
			{
				break;
			}
		}
	}

	for (l = 0; l < Lie; l++)
	{
		char a = borad[0][l];
		char b = borad[0][l + 1];
		int i = 1;
		int y = 0;
		while (i)
		{
			Compare(a, b, &i, l,&y);
			if (y == 1)
			{
				*x = 1;
			}
			if (l + 1 == Lie)
			{
				break;
			}
		}
	}

	for (h = 0; h < Hang; h++)
	{
		char a = borad[h][h];
		char b = borad[h + 1][h + 1];
		int i = 1;
		int y = 0;
		while (i)
		{
			Compare(a, b, &i, h,&y);
			if (y == 1)
			{
				*x = 1;
			}
			if (h + 1 == Hang)
			{
				break;
			}
		}
	}
}


//比较数组里的字符是否相等
void Compare(char a, char b, int* i, int p,int* y)
{
	if (a == '*' && a == b)
	{
		if (p + 1 == Hang)
		{
			printf("玩家赢了");
			*y = 1;
			*i = 0;
		}
		*i = 0;
	}
	else if (a == ' ' && b == ' ')
	{
		*i = 0;
	}
	else if (a == '#' && a == b)
	{
		if (p + 1 == Hang)
		{
			printf("电脑赢了");
			*y = 1;
			*i = 0;
		}
		*i = 0;
	}
	else
	{
		
			*i = 0;
		
	}
}